﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SD.DataAccess
{
    public class UserDao
    {
        static ServiceDBDataContext Context = new ServiceDBDataContext();

        /// <summary>
        /// get user object form user entity in DB
        /// </summary>
        /// <param name="account">account of user that want to get</param>
        /// <returns></returns>
        public static User GetUser(string account)
        {
            //check exist of user, if not exist return null
            if (Context.Users.Any(u => u.Account == account))
            {
                //if exist, get user from DB and return user
                User user = Context.Users.Single(u => u.Account == account);
                return user;
            }
            else
                return null;
        }

        /// <summary>
        /// update password of user
        /// </summary>
        /// <param name="account">account of user</param>
        /// <param name="password">new password that want to change</param>
        /// <returns></returns>
        public static bool UpdatePassword(string account, string password)
        {
            try
            {
                //check exist of user
                if (Context.Users.Any(u => u.Account == account))
                {
                    //if exist, get user object form DB
                    var users = Context.Users.First(u => u.Account == account);
                    // change password of user and save into DB
                    users.Password = password;
                    Context.SubmitChanges();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }

        }

    }
}
